package com.sne.airways.basecore.utils;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-20
 * Time: 上午11:37
 * To change this template use File | Settings | File Templates.
 */
public class DateJsonValueProcessor implements JsonValueProcessor {
    /**
     * DATEPATTERN
     */
    private String DATEPATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * JsonDateValueProcessor
     */
    public DateJsonValueProcessor() {
        super();
    }

    /**
     * @param format
     */
    public DateJsonValueProcessor(String format) {
        super();
        this.DATEPATTERN = format;
    }

    /**
     * @param value
     * @param jsonConfig
     * @return Object
     */
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {
        return process(value);
    }

    /**
     * @param key
     * @param value
     * @param jsonConfig
     * @return Object
     */
    public Object processObjectValue(String key, Object value,
                                     JsonConfig jsonConfig) {
        return process(value);
    }

    /**
     * process
     * @param value
     * @return
     */
    private Object process(Object value) {
        try {
            if (value instanceof Date) {
                SimpleDateFormat sdf = new SimpleDateFormat(DATEPATTERN,
                        Locale.UK);
                return sdf.format((Date) value);
            }
            return value == null ? "" : value.toString();
        } catch (Exception e) {
            return "";
        }

    }

    /**
     * @return the DATEPATTERN
     */
    public String getDATEPATTERN() {
        return DATEPATTERN;
    }

    /**
     * @param pDatePattern the DATEPATTERN to set
     */
    public void setDATEPATTERN(String pDatePattern) {
        DATEPATTERN = pDatePattern;
    }
}
